package cn.com.zcode.user.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import cn.com.zcode.core.service.BaseServiceSupport;
import cn.com.zcode.core.util.HttpReqUtil;
import cn.com.zcode.core.util.StringUtil;
import cn.com.zcode.email.service.EmailService;
import cn.com.zcode.user.dao.UserDao;
import cn.com.zcode.user.model.InviteCode;
import cn.com.zcode.user.model.LoginInfo;
import cn.com.zcode.user.model.User;
import cn.com.zcode.user.util.UserUtil;

import com.opensymphony.xwork2.ActionContext;

public class UserServiceImpl extends BaseServiceSupport<UserDao> implements UserService {
	private String emailKey;
	private EmailService emailService;

	public User getUser(String username, String password, boolean md5) {
		password = (password + "").trim();
		username = (username + "").trim();
		if (md5) {
			password = StringUtil.getMd5Value(password);

		}
		return this.mainDao.getUser(username, password);
	}

	@Override
	public User getUserById(String id) {

		return this.mainDao.getUserById(id);
	}

	@Override
	public User getUserByName(String name) {
		return this.mainDao.getUserByName(name);
	}

	public LoginInfo addLoginInfo(LoginInfo logininfo) {
		return this.mainDao.save(logininfo);
	}

	@Override
	public boolean getInviteCode(String inviteCode) {

		return this.mainDao.checkInviteCode(inviteCode);
	}

	@Override
	public void updateInviteCode(String userid, String inviteCode) {
		this.mainDao.usedInviteCode(userid, inviteCode);
	}

	@Override
	public User addUser(User user, String inviteCode) {
		this.saveUser(user);
		this.updateInviteCode(user.getId(), inviteCode);
		return user;
	}

	@Override
	public User saveUser(User user) {
		String pwd = user.getPassword();
		if (pwd != null) {
			String md5pwd = StringUtil.getMd5Value(pwd.trim());
			user.setPasswordMD5(md5pwd);
		}
		return this.mainDao.save(user);
	}

	public EmailService getEmailService() {
		return emailService;
	}

	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	@Override
	public InviteCode addInviteCode(InviteCode code) {
		InviteCode CodeinviteCode = this.mainDao.save(code);
		String to = code.getEmail();
		Map<String, Object> datas = new HashMap<String, Object>();
		datas.put("invite", code);
		datas.put("date", new Date());
		this.emailService.send(to, null, emailKey, datas);
		return CodeinviteCode;
	}

	@Override
	public List<InviteCode> listInviteCodes() {
		return this.mainDao.listInviteCodes();
	}

	public String getEmailKey() {
		return emailKey;
	}

	public void setEmailKey(String emailKey) {
		this.emailKey = emailKey;
	}

	@Override
	public void initUser(User user, HttpServletRequest request,HttpServletResponse response,boolean addcookie) {
		try {
			HttpSession session = request.getSession();
			boolean isFromMobile = HttpReqUtil.isFromMobile(request);
			user.setFromMobile(isFromMobile);
			session.setAttribute(User.SESSION_USER, user);
			session.setAttribute(User.SESSION_USER_JSON, JSONUtil.serialize(user));

			String ip = UserUtil.getUserIp(request);
			LoginInfo logininfo = new LoginInfo(user, ip);
			this.addLoginInfo(logininfo);
			if (addcookie) {
				UserUtil.addCookieUser(response, user);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	@Override
	public void destroyUser(User user) {
		ActionContext context = ActionContext.getContext();
		HttpServletResponse response = ServletActionContext.getResponse();
		Map<String, Object> session = context.getSession();
		session.remove(User.SESSION_USER);
		session.remove(User.SESSION_USER_JSON);
		UserUtil.removeCookieUser(response);
	}

	@Override
	public List<User> findUsers(String...userid) {
		 
		return this.mainDao.findUsers(userid);
	}

	@Override
	public List<User> findAll() {
		return this.mainDao.findAll(User.class);
	}
}
